#include "CMainMenu.h"


CMainMenu::CMainMenu(IrrlichtDevice *device)
{
	driver = device->getVideoDriver();
	smgr = device->getSceneManager();

	// Load background texture
	background = driver->getTexture(L"data/textures/menu/background.jpg");
	
	// Load font
	font = device->getGUIEnvironment()->getFont(L"data/fonts/myfont.xml");
	
	// Add menu items at array
	menuItem.push_back("Play");
	menuItem.push_back("About");
	menuItem.push_back("Exit");

	// Set current selected item
	selectedItem = PLAY;
}


CMainMenu::~CMainMenu(void)
{
}


void CMainMenu::update()
{
	driver->draw2DImage(background, recti(0, 0, driver->getScreenSize().Width, driver->getScreenSize().Height), 
		recti(0, 0, background->getOriginalSize().Width, background->getSize().Height));
	
	// Draw menu items
	for (u32 i = 0; i < menuItem.size(); ++i)
	{
		// Calculate position of the menu items
		recti position = recti(
			driver->getScreenSize().Width/2 - font->getDimension(menuItem[i].c_str()).Width/2, 
			driver->getScreenSize().Height/2 + i * font->getDimension(menuItem[i].c_str()).Height + 20, 
			font->getDimension(menuItem[i].c_str()).Width, 
			font->getDimension(menuItem[i].c_str()).Height);
		
		SColor color;
		
		// Selected item is highlighted in red. Other items is yellow
		if(i != selectedItem)
		{
			color = SColor(255, 214, 232, 11);
		}
		else
		{
			color = SColor(255, 203, 1, 5);
		}

		font->draw(menuItem[i].c_str(), position, color);
	}
}


void CMainMenu::cursorUp()
{
	if (selectedItem != 0)
	{
		selectedItem--;
	}
	else
	{
		selectedItem = menuItem.size() - 1;
	}
}


void CMainMenu::cursorDown()
{
	if (selectedItem != (menuItem.size() - 1))
	{
		selectedItem++;
	}
	else
	{
		selectedItem = 0;
	}
}


// Draws 'Pause' on the screen
void CMainMenu::drawPause()
{
	recti position = recti(
		driver->getScreenSize().Width/2 - font->getDimension(L"Pause").Width/2, 
		driver->getScreenSize().Height/2 - font->getDimension(L"Pause").Height/2, 
		font->getDimension(L"Pause").Width, 
		font->getDimension(L"Pause").Height);

	font->draw(L"Pause", position, SColor(255, 214, 232, 11), false, false, 0);
}


int CMainMenu::getSelectedItem()
{
	return selectedItem;
}
